package com.sl.mes.app.service.puboid;


import com.sl.mes.app.controller.ApiRequestTools;
import com.sl.mes.db.itf.puboid.IPubOid;
import com.sl.mes.db.vo.PubOid;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class PubOidService {

    public static long OID_BASE_INITIAL_VAL = 19000000000000L;

    String dataSource = "dsign";

    @Resource
    private IPubOid iPubOid;

    /**
     * 查询
     *
     * @param group
     * @return
     */
    public PubOid getPubOidById(String group) {
        PubOid pubOid = iPubOid.findPubOidById(group);
        return pubOid;
    }

    /**
     * 删除
     *
     * @param group
     */
    public int deletePubOidById(String group) {
        return iPubOid.deletePubOidById(group);
    }

    /**
     * 添加
     */
    public PubOid insertPubOidById(PubOid pubOid) {
        return iPubOid.insertPubOid(pubOid);
    }

    /**
     * 修改
     *
     * @param pubOid
     */
    public int updataPubOidVoById(PubOid pubOid) {
        return iPubOid.updataPubOid(pubOid);
    }


    public String generate() {
        String groupNmber = "0001";
        String ds = this.dataSource;
        return OidGenerator.getInstance().nextOid(ds, groupNmber);
    }

    public String[] generate(int amount) {
        String groupNmber = "0001";
        String[] newOids = new String[amount];
        String ds = this.dataSource;
        for (int i = 0; i < amount; i++) {
            newOids[i] = OidGenerator.getInstance().nextOid(ds, groupNmber);
        }
        return newOids;
    }


    public String generate(String pk_corp) {
        if (pk_corp == null) {
            throw new IllegalArgumentException("Can't generate primary key with null pk_corp");
        }

        return OidGenerator.getInstance().nextOid(this.dataSource, pk_corp);
    }


    public String[] generate(String pk_corp, int amount) {
        if (pk_corp == null) {
            throw new IllegalArgumentException("Can't generate primary key with null pk_corp");
        }

        String[] newOids = new String[amount];
        if (1 == 2) {
            for (int i = 0; i < amount; i++) {
                OID_BASE_INITIAL_VAL += 1L;
                newOids[i] = OidBaseAlgorithm.getInstance(String.valueOf(OID_BASE_INITIAL_VAL)).nextOidBase();
            }

            return newOids;
        }

        if (this.dataSource == null) {
            for (int i = 0; i < amount; i++) {
                newOids[i] = OidGenerator.getInstance().nextOid(pk_corp);
            }
        } else {
            for (int i = 0; i < amount; i++) {
                newOids[i] = OidGenerator.getInstance().nextOid(this.dataSource, pk_corp);
            }
        }
        return newOids;
    }


    /**
     * 新增
     *
     * @param pk_corp
     * @param oid
     * @throws Exception
     */
    private void insertOidBase(String pk_corp, String oid) {
        //"insert into PUB_OID (group, pub_oid) values(?,?)"

        try {
            ApiRequestTools.insertOid(pk_corp, oid);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String selectForUpdateOidBase(String pk_corp) {

        String currentId = null;
        String firstId = "10000000000000";

        //TODO:先从数据库里查询id
        try {
            String strOid = ApiRequestTools.getOid();

            if (!strOid.equals("")) {
                currentId = strOid;
                if (currentId.compareTo("10000000000000") < 0) {
                    currentId = firstId;
                }

                //用数据库里查询出来的id，查找一个id
                currentId = nextOidCode(currentId);

                //再更新Id
                //("update PUB_OID set pub_oid=? where group=?");
                ApiRequestTools.updateOid(pk_corp, currentId);


            } else {
                //新增
                ApiRequestTools.insertOid(pk_corp, firstId);
                currentId = firstId;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return currentId;
    }

    private String nextOidCode(String currentId) {
        OidBaseAlgorithm oidBaseAlgrithm = OidBaseAlgorithm.getInstance(currentId);
        String nextOidBaseCode = null;

        for (int j = 0; j < 1000; j++) {
            nextOidBaseCode = oidBaseAlgrithm.nextOidBase();
        }
        return nextOidBaseCode;
    }


}
